package com.zzj.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zzj.entity.Product;
import com.zzj.service.IProductService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 读取Excel监视器
 */
@Component
public class ProductListener extends AnalysisEventListener<Product> {

  @Resource
  private IProductService iProductService;

  // 这个每一条数据解析都会来调用
  @Override
  public void invoke(Product product, AnalysisContext analysisContext) {
    try {
      if (Objects.equals(product.getName(), "")
          || product.getPrice() == null
          || product.getName() == null) {
      } else {
        iProductService.save(product);
      }

    } catch (Exception ex) {

    }

  }

  // 所有数据解析完成了 都会来调用
  @Override
  public void doAfterAllAnalysed(AnalysisContext analysisContext) {

  }
}
